<?php

/* Tangozebra Web Site CMS Insert Sector Update
 * (c)2003 Digital Advertising and Marketing Ltd
 */

// require("../session.inc");
// require("../include.inc");
require("../include/functions.inc");
// require_once("verify_login.phtml"); //check if the user is logged in, include this one every access restricted page

/*
header("Content-Type: text/plain");
foreach (array_keys($_POST) as $k) {
  print "$k = {$_POST[$k]}\n";
}
exit;
*/

//////////////////////////////////////////////////////////////////////
// DB connection
//////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////
// Send newsletter
//////////////////////////////////////////////////////////////////////

function multipart_mail($From,$FromName,$To,$Subject,$Text,$Html){ // returns nothing
	$OB="----=_OuterBoundary_000";
	$IB="----=_InnerBoundery_001";
	$Html=$Html?$Html:preg_replace("/\n/","{br}",$Text) or die("neither text nor html part present.");
	$Text=$Text?chunk_split(base64_encode($Text)):"Sorry, but you need an html mail client to read this mail.";
	$From or die("sender address missing");
	$To or die("recipient address missing");
	
	$headers ="MIME-Version: 1.0\r\n"; 
	$headers.="From: ".$FromName." <".$From.">\n"; 
	$headers.=$To; 
	$headers.="Reply-To: ".$From; 
	$headers.="X-Priority: 3\n"; 
	$headers.="X-Mailer: PHP / ".phpversion()."\n"; 
	$headers.="Content-Type: multipart/mixed;\n\tboundary=\"".$OB."\"\n";

	//Messages start with text/html alternatives in OB
	$Msg ="This is a multi-part message in MIME format.\n";
	$Msg.="\n--".$OB."\n";
	$Msg.="Content-Type: multipart/alternative;\n\tboundary=\"".$IB."\"\n\n";

	//plaintext section 
	$Msg.="\n--".$IB."\n";
	$Msg.="Content-Type: text/plain;\n\tcharset=\"utf-8\"\n";
	//$Msg.="Content-Transfer-Encoding: quoted-printable\n\n";
	$Msg.="Content-Transfer-Encoding: base64\n\n";
	// plaintext goes here
	$Msg.=$Text."\n\n";

	// html section 
	$Msg.="\n--".$IB."\n";
	$Msg.="Content-Type: text/html;\n\tcharset=\"utf-8\"\n";
	$Msg.="Content-Transfer-Encoding: base64\n\n";
	// html goes here 
	$Msg.=chunk_split(base64_encode($Html))."\n\n";
	
	// end of IB
	$Msg.="\n--".$IB."--\n";

	$Msg.="\n--".$OB."--\n";
	mail($To,$Subject,$Msg,$headers);
}

//send test newsletter
if (!$sendtoall) {
  // $html = stripslashes($htmlcode);
  multipart_mail($fromemail,$from,$to,$subject,$plaintext,stripslashes($htmlcode));
  // redirect
  header("Location: pismo.php?confirmation=true");
  exit;
} else {
	openConnection();
	$sql = "select email from pismo";
	$res = mysql_query ($sql)
	or die("No delition. Try again sunser!");
	while ($row = mysql_fetch_array($res)) {
		$to = $row['email'];
		multipart_mail($fromemail,$from,$to,$subject,$plaintext,stripslashes($htmlcode));
	}
	header("Location: pismo.php?confirmation=true");
  	exit;
}
?>